//
//  DLLoginViewController.m
//  FRP_OC
//
//  Created by DeLongYang on 2017/10/30.
//  Copyright © 2017年 DeLongYang. All rights reserved.
/*
    参照简书地址：http://www.jianshu.com/p/e10e5ca413b7
    实验的效果。感觉还是不错的
 */

#import "DLLoginViewController.h"
#import <ReactiveObjC/ReactiveObjC.h>
#import "LoginViewModel.h"

@interface DLLoginViewController ()

@property (nonatomic,strong)LoginViewModel *loginViewModel;

@property (weak, nonatomic) IBOutlet UITextField *accountField;

@property (weak, nonatomic) IBOutlet UITextField *pwField;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@end

@implementation DLLoginViewController

- (void)viewDidLoad {
    //
    [super viewDidLoad];
    [self bindeModel];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (LoginViewModel *)loginViewModel
{
    if (_loginViewModel == nil) {
        _loginViewModel = [[LoginViewModel alloc] init];
    }
    return  _loginViewModel;
}

#pragma mark ---- 初始化 MVVM
/**
 绑定模型
 */
- (void)bindeModel
{
    // 给模型的属性绑定信号
    // 只要账号文本框一改变，就会给account赋值
    RAC(self.loginViewModel.account,account) = _accountField.rac_textSignal;
    RAC(self.loginViewModel.account,pwd) = _pwField.rac_textSignal;
    
    // 绑定登录按钮
    RAC(self.loginBtn,enabled) = self.loginViewModel.enableLoginSignal;
    
    // 监听登录按钮点击
    [[_loginBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [self.loginViewModel.loginCommand execute:nil];
    }];
    
}








@end
